//
//  MCPhotoGroupView.m
//  imageViewCHENK
//
//  Created by 李李善 on 2018/1/20.
//  Copyright © 2018年 李李善. All rights reserved.
//

#import "MCPhotoGroupView.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "MWPhotoBrowser.h"


@interface MCPhotoGroupViewCell : UICollectionViewCell

@property(nonatomic,strong)UIImageView *imageView;
@end

@implementation MCPhotoGroupViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        self.layer.masksToBounds = YES;
        
        UIImageView *imageV= [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        imageV.backgroundColor =[self randomColor];
        [self addSubview:imageV];
        _imageView = imageV;
        
    }
    return self;
}

-(UIColor *)randomColor{
    
    NSInteger randRed = arc4random()%255;
    NSInteger randBlue = arc4random()%255;
    NSInteger randGreen = arc4random()%255;
    return [UIColor colorWithRed:randRed/255.0 green:randBlue/255.0 blue:randGreen/255.0 alpha:1.0];
}

@end

@interface MCPhotoGroupView()<UICollectionViewDelegate,UICollectionViewDataSource,MWPhotoBrowserDelegate>

@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *photos;

@end

@implementation MCPhotoGroupView



- (instancetype)initWIthSize:(CGSize)size Interitem:(CGFloat)interitem Line:(CGFloat)line
{
    return [self initWIthSize:size Interitem:interitem Line:line DataBlock:nil];
   
}

- (instancetype)initWIthSize:(CGSize)size Interitem:(CGFloat)interitem Line:(CGFloat)line DataBlock:(dataBlock)block
{
    self = [super init];
    if (self) {
        
    
        UICollectionView *cV = ({
            UICollectionViewFlowLayout *layou = [[UICollectionViewFlowLayout alloc]init];
            layou.scrollDirection =UICollectionViewScrollDirectionVertical;
            layou.itemSize = size;
            layou.minimumInteritemSpacing = interitem;
            layou.minimumLineSpacing = line;
            UICollectionView*l=[[ UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layou];
            [l registerClass:[MCPhotoGroupViewCell class] forCellWithReuseIdentifier:@"cell"];
            l.backgroundColor = self.backgroundColor;
            l.showsHorizontalScrollIndicator = NO;
            l.showsVerticalScrollIndicator = NO;
//            l.bounces = NO;
            l.dataSource = self;
            l.delegate =self;
            l.backgroundColor = self.backgroundColor;
            [self addSubview:l];
            l;
        });
        _collectionView = cV;
        
        if (block) {
            self.block = block;
        }

        
    }
    return self;
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MCPhotoGroupViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    id data = self.dataArr[indexPath.row];
    if ([data isKindOfClass:[UIImage class]]) {
        cell.imageView.image = data;
    }else if ([data isKindOfClass:[NSString class]])
    {
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:data] placeholderImage:nil];
    }
    else if ([data isKindOfClass:[NSData class]])
    {
        cell.imageView.image = [UIImage imageWithData:data];
    }
    else
    {
        if (self.block) {
            self.block(data,cell.imageView);
        }
        
    }
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [_photos  removeAllObjects];
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    browser.displayActionButton = NO;
    browser.alwaysShowControls = NO;
    browser.displaySelectionButtons = NO;
    browser.zoomPhotosToFill = YES;
    browser.displayNavArrows = NO;
    browser.startOnGrid = NO;
    browser.enableGrid = YES;
    for (id data in _dataArr) {
        MWPhoto *photo;
        if ([data isKindOfClass:[UIImage class]]) {
       photo = [MWPhoto photoWithImage:data];
        }else if ([data isKindOfClass:[NSString class]])
        {
            photo = [MWPhoto photoWithURL:[NSURL URLWithString:data]];
        }
        [_photos addObject:photo];
    }
    [browser setCurrentPhotoIndex:indexPath.row];
    [[self viewController].navigationController pushViewController:browser animated:YES];
    
}

- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return [self getTopController];
}
- (UIViewController *)getTopController{
    
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }
    
    return topController;
}

#pragma mark - photobrowser代理方法
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return self.photos.count;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < self.photos.count) {
        return [self.photos objectAtIndex:index];
    }
    return nil;
}

- (void)updateData:(id)data
{
    [self.dataArr removeAllObjects];
    [self.dataArr addObjectsFromArray:data];
    [self.collectionView reloadData];
}


-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

-(NSMutableArray *)photos
{
    if (!_photos) {
        _photos = [NSMutableArray array];
    }
    return _photos;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    _collectionView.frame = CGRectMake(0, 0,self.bounds.size.width, self.bounds.size.height);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
